package hn.cch.springboot_datasource.dao.impl;

import hn.cch.springboot_datasource.dao.UserDao;
import hn.cch.springboot_datasource.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userMysqlDaoImpl")
public class UserMysqlDaoImpl implements UserDao {


    @Autowired
    @Qualifier("mysqlJdbcTemplate")
    private JdbcTemplate mysqlJdbcTemplate;


    public int insertUser(User user) {
        String sql = "insert into tbl_user(user_name, number, password) values (?,?,?)";
        Object[] params = new Object[]{user.getUserName(), user.getNumber(), user.getPassword()};
        int row = mysqlJdbcTemplate.update(sql, params);
        return row;
    }


    @Override
    public List<User> selectAllUserList() {

        String sql = "select user_id, user_name, number, password from tbl_user";
        List<User> userList = mysqlJdbcTemplate.query(sql, (resultSet, i) -> {
            User user = new User();
            user.setUserId(resultSet.getInt("user_id"));
            user.setUserName(resultSet.getString("user_name"));
            user.setNumber(resultSet.getInt("number"));
            user.setPassword(resultSet.getString("password"));
            return user;
        });

        return userList;
    }

    @Override
    public List<User> callUser(String userName) {
        return null;
    }
}
